package com.xiaoyu.commonlib.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;

import com.xiaoyu.commonlib.R;

import java.io.IOException;

import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;

public class LoadingDialog extends Dialog {
    private GifImageView mScanning;

    private EmptyDialogDismissListener mListener;
    public static final int DISMISS_REASON_NORMAL = 0;
    public static final int DISMISS_REASON_BACK = 1;

    public LoadingDialog(Context context, boolean cover) {
        super(context, cover ? 0 : R.style.DialogNoCover);
        final View view = LayoutInflater.from(context).inflate(
                R.layout.common_scanning_dialog, null);
        mScanning = (GifImageView) view.findViewById(R.id.dialog_loading);
        GifDrawable gifDrawable = null;
        try {
            gifDrawable = new GifDrawable(context.getResources(), R.mipmap.loading);
            gifDrawable.setLoopCount(0);
            gifDrawable.setSpeed(2);
            mScanning.setImageDrawable(gifDrawable);
        } catch (IOException e) {

        }

        View background = view.findViewById(R.id.dialog_bg);
        background.setVisibility(cover ? View.VISIBLE : View.INVISIBLE);

        setContentView(view);
        setCancelable(false);

        Window window = this.getWindow();
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        window.setGravity(Gravity.CENTER);

        try {
            int dividerID = context.getResources().getIdentifier("android:id/titleDivider", null, null);
            View divider = findViewById(dividerID);
            divider.setBackgroundColor(Color.TRANSPARENT);
        } catch (Exception e) {
            //上面的代码，是用来去除Holo主题的蓝色线条
        }

        setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialogInterface, int keyCode, KeyEvent keyEvent) {
                if (keyCode == KeyEvent.KEYCODE_BACK && keyEvent.getAction() == KeyEvent.ACTION_UP) {
                    if (mListener != null) {
                        mListener.onDismiss(DISMISS_REASON_BACK);
                    }
                }
                return false;
            }
        });
    }

    @Override
    public void show() {
        super.show();
    }

    public void dismissLoadingDialog() {
        dismiss();
        if (mListener != null) {
            mListener.onDismiss(DISMISS_REASON_NORMAL);
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    public interface EmptyDialogDismissListener {
        void onDismiss(int reason);
    }

    public void setEmptyDismissListener(EmptyDialogDismissListener listener) {
        mListener = listener;
    }
}